{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from lightgbm.sklearn import LGBMClassifier\n",
    "from sklearn.metrics import accuracy_score, auc, roc_auc_score\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import KFold\n",
    "## 0. 打印设置\n",
    "pd.set_option('display.max_columns', None)\n",
    "# pd.set_option('display.max_rows', None)  ## 显示全部结果，不带省略点\n",
    "# pd.set_option('display.width', 1000)\n",
    "pd.set_option('display.float_format', '{:.0f}'.format)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 1.1 读取数据\n",
    "train_Base = pd.read_csv(r\"data/train.csv\")\n",
    "test_Base = pd.read_csv(r\"data/test.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 1.2 数据合并\n",
    "# data = pd.concat([test_Base, train_Base], axis=0)\n",
    "# data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 1.3 数据清洗\n",
    "## 1.3.1 索引完善\n",
    "# data.index = range(len(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 1.4 数据探索\n",
    "## 1.4.1 空值数量\n",
    "# data.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 1.4.2 唯一值个数\n",
    "# for col in data.columns:\n",
    "#     print(col, data[col].nunique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 1.4.3 字符串的字段，唯一值统计\n",
    "# cat_columns = data.select_dtypes(include='object').columns  \n",
    "\n",
    "# column_name = []\n",
    "# unique_value = []\n",
    " \n",
    "# for col in cat_columns:\n",
    "#     column_name.append(col)\n",
    "#     unique_value.append(data[col].nunique())\n",
    "\n",
    "# df = pd.DataFrame()\n",
    "# df['col_name'] = column_name\n",
    "# df['value'] = unique_value\n",
    "# df = df.sort_values('value', ascending=False)\n",
    " \n",
    "# df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 2 特征工程\n",
    "## 2.0 特征编码--property_damage、police_report_available\n",
    "# data['property_damage'].value_counts()\n",
    "# data['property_damage'] = data['property_damage'].map({'NO': 0, 'YES': 1, '?': 2})\n",
    "# data['property_damage'].value_counts()\n",
    "\n",
    "# data['police_report_available'].value_counts()\n",
    "# data['police_report_available'] = data['police_report_available'].map({'NO': 0, 'YES': 1, '?': 2})\n",
    "# data['police_report_available'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ## 2.2 去除无关的特征\n",
    "# data.drop(['policy_id'], axis=1, inplace=True)\n",
    "# data.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 2.3 标签编码\n",
    "cat_columns = train_Base.select_dtypes(include=['object']).columns\n",
    "le = LabelEncoder()\n",
    "for col in cat_columns:\n",
    "    train_Base[col] = le.fit_transform(train_Base[col])\n",
    "    test_Base[col] = le.fit_transform(test_Base[col])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 2.4 分箱编码\n",
    "\n",
    "# ## 1）age分箱\n",
    "# for x in range(10,70,10):\n",
    "#     train_Base[train_Base['age'].between(x,x+10)].loc[:,['age']]=x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "## 3. 数据集切分\n",
    "## 3.1 切分训练集和测试集\n",
    "# train = data[data['fraud'].notnull()]\n",
    "# test = data[data['fraud'].isnull()]\n",
    "X = train_Base.drop(columns=['policy_id', 'fraud'])\n",
    "Y = train_Base['fraud']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 3.2 训练集中，训练集和验证集的划分\n",
    "\n",
    "# x_train, x_train_01 = train_test_split(train.drop(['fraud'],axis=1), test_size=0.2, random_state=42)  # 25% of remaining data as validation set  \n",
    "# y_train, y_train_01 = train_test_split(train['fraud'], test_size=0.2, random_state=42)  # Split labels accordingly  \n",
    "\n",
    "x_train, x_train_01, y_train, y_train_01 = train_test_split(X, Y, test_size=0.2, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 4. 模型训练\n",
    "## 4.1 建立模型\n",
    "gbm = LGBMClassifier(n_estimators=600, learning_rate=0.01, boosting_type='gbdt',  ## 模型训练超参数 调优参考：https://blog.51cto.com/u_16213313/7201851\n",
    "                     objective='binary',   ## LGBMClassifier详解： https://blog.csdn.net/yeshang_lady/article/details/118638269\n",
    "                     max_depth=-1,\n",
    "                     random_state=2022,\n",
    "                     metric='auc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LGBMClassifier(learning_rate=0.01, metric='auc', n_estimators=600,\n",
       "               objective='binary', random_state=2022)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.2 模型训练\n",
    "## train.drop(['fraud'],axis=1) ## axis=0 表示行，axis=1 表示列\n",
    "gbm.fit(x_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 4.3 模型预测，以proba进行提交，结果会更好\n",
    "y_train_01_pred = gbm.predict_proba(x_train_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "auc值: 0.825578231292517\n",
      "Accuracy: 0.8214285714285714\n",
      "Precision: 0.631578947368421\n",
      "Recall: 0.6857142857142857\n",
      "F1 Score: 0.6575342465753424\n"
     ]
    }
   ],
   "source": [
    "## 5. 模型评估\n",
    "## 5.1 评估auc值\n",
    "auc = roc_auc_score(y_train_01, y_train_01_pred[:,-1]) # 计算auc值\n",
    "print(\"auc值:\", auc)\n",
    "v_code = str(round(auc,5)).split('.')[1]\n",
    "\n",
    "## 5.2 概率转换\n",
    "y_train_01_pred[:, 1][y_train_01_pred[:, 1] > 0.5] = '1'\n",
    "y_train_01_pred[:, 1][y_train_01_pred[:, 1] <= 0.5] = '0'\n",
    "y_train_01_pred\n",
    "\n",
    "## 5.3 评估accuracy，precision，recall，f1\n",
    "from sklearn.metrics import precision_score, recall_score, f1_score\n",
    " \n",
    "accuracy=accuracy_score(y_train_01, y_train_01_pred[:,-1])  ## 计算准确率\n",
    "precision = precision_score(y_train_01, y_train_01_pred[:,-1]) # 计算精确率\n",
    "recall = recall_score(y_train_01, y_train_01_pred[:,-1]) # 计算召回率\n",
    "f1 = f1_score(y_train_01, y_train_01_pred[:,-1]) # 计算F1值\n",
    "\n",
    "\n",
    "# 输出计算得到的准确率、召回率和F1值\n",
    "print(\"Accuracy:\", accuracy)\n",
    "print(\"Precision:\", precision)\n",
    "print(\"Recall:\", recall)\n",
    "print(\"F1 Score:\", f1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ## 5.1 模型命名，版本控制\n",
    "model_name=f'model_0_{v_code}_base'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.9632268 , 0.        ],\n",
       "       [0.36388375, 1.        ],\n",
       "       [0.98821778, 0.        ],\n",
       "       [0.95145253, 0.        ],\n",
       "       [0.98189911, 0.        ],\n",
       "       [0.95605624, 0.        ],\n",
       "       [0.97538168, 0.        ],\n",
       "       [0.9850762 , 0.        ],\n",
       "       [0.98756046, 0.        ],\n",
       "       [0.98260209, 0.        ],\n",
       "       [0.98987076, 0.        ],\n",
       "       [0.98399295, 0.        ],\n",
       "       [0.26981446, 1.        ],\n",
       "       [0.94720864, 0.        ],\n",
       "       [0.99033894, 0.        ],\n",
       "       [0.60745107, 0.        ],\n",
       "       [0.90867083, 0.        ],\n",
       "       [0.94976572, 0.        ],\n",
       "       [0.13030434, 1.        ],\n",
       "       [0.97556139, 0.        ],\n",
       "       [0.97249955, 0.        ],\n",
       "       [0.86674971, 0.        ],\n",
       "       [0.48363067, 1.        ],\n",
       "       [0.91932692, 0.        ],\n",
       "       [0.96334722, 0.        ],\n",
       "       [0.17758365, 1.        ],\n",
       "       [0.99505919, 0.        ],\n",
       "       [0.96957817, 0.        ],\n",
       "       [0.44375565, 1.        ],\n",
       "       [0.99350068, 0.        ],\n",
       "       [0.65172497, 0.        ],\n",
       "       [0.96080754, 0.        ],\n",
       "       [0.14393372, 1.        ],\n",
       "       [0.9759898 , 0.        ],\n",
       "       [0.97413795, 0.        ],\n",
       "       [0.98825965, 0.        ],\n",
       "       [0.17163285, 1.        ],\n",
       "       [0.99787197, 0.        ],\n",
       "       [0.99118484, 0.        ],\n",
       "       [0.91236085, 0.        ],\n",
       "       [0.31753861, 1.        ],\n",
       "       [0.99208878, 0.        ],\n",
       "       [0.99050889, 0.        ],\n",
       "       [0.97764896, 0.        ],\n",
       "       [0.73579735, 0.        ],\n",
       "       [0.93840474, 0.        ],\n",
       "       [0.99656847, 0.        ],\n",
       "       [0.98290518, 0.        ],\n",
       "       [0.18278958, 1.        ],\n",
       "       [0.98433317, 0.        ],\n",
       "       [0.96494312, 0.        ],\n",
       "       [0.96860861, 0.        ],\n",
       "       [0.99097502, 0.        ],\n",
       "       [0.39947093, 1.        ],\n",
       "       [0.2361514 , 1.        ],\n",
       "       [0.98638003, 0.        ],\n",
       "       [0.98923632, 0.        ],\n",
       "       [0.39003567, 1.        ],\n",
       "       [0.9779119 , 0.        ],\n",
       "       [0.46859783, 1.        ],\n",
       "       [0.99013124, 0.        ],\n",
       "       [0.97677514, 0.        ],\n",
       "       [0.94174529, 0.        ],\n",
       "       [0.12364456, 1.        ],\n",
       "       [0.96085991, 0.        ],\n",
       "       [0.92352275, 0.        ],\n",
       "       [0.4827531 , 1.        ],\n",
       "       [0.40170165, 1.        ],\n",
       "       [0.99125527, 0.        ],\n",
       "       [0.94816654, 0.        ],\n",
       "       [0.98058344, 0.        ],\n",
       "       [0.986314  , 0.        ],\n",
       "       [0.61337865, 0.        ],\n",
       "       [0.21091842, 1.        ],\n",
       "       [0.99474149, 0.        ],\n",
       "       [0.98529491, 0.        ],\n",
       "       [0.92861685, 0.        ],\n",
       "       [0.98984053, 0.        ],\n",
       "       [0.51154647, 0.        ],\n",
       "       [0.98922834, 0.        ],\n",
       "       [0.97932904, 0.        ],\n",
       "       [0.99378139, 0.        ],\n",
       "       [0.7314142 , 0.        ],\n",
       "       [0.98668811, 0.        ],\n",
       "       [0.95991951, 0.        ],\n",
       "       [0.99309131, 0.        ],\n",
       "       [0.92898178, 0.        ],\n",
       "       [0.97339928, 0.        ],\n",
       "       [0.9731993 , 0.        ],\n",
       "       [0.43848581, 1.        ],\n",
       "       [0.98321774, 0.        ],\n",
       "       [0.30952865, 1.        ],\n",
       "       [0.07959726, 1.        ],\n",
       "       [0.96779379, 0.        ],\n",
       "       [0.99466908, 0.        ],\n",
       "       [0.98349899, 0.        ],\n",
       "       [0.90242109, 0.        ],\n",
       "       [0.98163583, 0.        ],\n",
       "       [0.68802444, 0.        ],\n",
       "       [0.97161161, 0.        ],\n",
       "       [0.98199644, 0.        ],\n",
       "       [0.34190723, 1.        ],\n",
       "       [0.99274767, 0.        ],\n",
       "       [0.96372302, 0.        ],\n",
       "       [0.99704802, 0.        ],\n",
       "       [0.40369983, 1.        ],\n",
       "       [0.53749508, 0.        ],\n",
       "       [0.24446293, 1.        ],\n",
       "       [0.98649562, 0.        ],\n",
       "       [0.98759819, 0.        ],\n",
       "       [0.96695813, 0.        ],\n",
       "       [0.94893326, 0.        ],\n",
       "       [0.98524226, 0.        ],\n",
       "       [0.99099277, 0.        ],\n",
       "       [0.99116141, 0.        ],\n",
       "       [0.55193732, 0.        ],\n",
       "       [0.09154213, 1.        ],\n",
       "       [0.99645954, 0.        ],\n",
       "       [0.07606516, 1.        ],\n",
       "       [0.98692136, 0.        ],\n",
       "       [0.96340381, 0.        ],\n",
       "       [0.94642923, 0.        ],\n",
       "       [0.16224841, 1.        ],\n",
       "       [0.62953223, 0.        ],\n",
       "       [0.9684048 , 0.        ],\n",
       "       [0.23796846, 1.        ],\n",
       "       [0.9243062 , 0.        ],\n",
       "       [0.98662172, 0.        ],\n",
       "       [0.40802499, 1.        ],\n",
       "       [0.98892203, 0.        ],\n",
       "       [0.99443638, 0.        ],\n",
       "       [0.41236295, 1.        ],\n",
       "       [0.98914116, 0.        ],\n",
       "       [0.98926146, 0.        ],\n",
       "       [0.98189044, 0.        ],\n",
       "       [0.98115071, 0.        ],\n",
       "       [0.97957319, 0.        ],\n",
       "       [0.19691762, 1.        ],\n",
       "       [0.46227127, 1.        ],\n",
       "       [0.99084573, 0.        ],\n",
       "       [0.92198619, 0.        ],\n",
       "       [0.96764941, 0.        ],\n",
       "       [0.45914935, 1.        ],\n",
       "       [0.94967222, 0.        ],\n",
       "       [0.25034354, 1.        ],\n",
       "       [0.33299117, 1.        ],\n",
       "       [0.98121297, 0.        ],\n",
       "       [0.99348074, 0.        ],\n",
       "       [0.69001511, 0.        ],\n",
       "       [0.97768708, 0.        ],\n",
       "       [0.9734607 , 0.        ],\n",
       "       [0.43671376, 1.        ],\n",
       "       [0.04545057, 1.        ],\n",
       "       [0.4217141 , 1.        ],\n",
       "       [0.99144204, 0.        ],\n",
       "       [0.97872791, 0.        ],\n",
       "       [0.26359167, 1.        ],\n",
       "       [0.96381679, 0.        ],\n",
       "       [0.91058619, 0.        ],\n",
       "       [0.99476077, 0.        ],\n",
       "       [0.33515167, 1.        ],\n",
       "       [0.90776868, 0.        ],\n",
       "       [0.25498477, 1.        ],\n",
       "       [0.40454811, 1.        ],\n",
       "       [0.58426223, 0.        ],\n",
       "       [0.18241239, 1.        ],\n",
       "       [0.98379989, 0.        ],\n",
       "       [0.99359233, 0.        ],\n",
       "       [0.18073839, 1.        ],\n",
       "       [0.96467376, 0.        ],\n",
       "       [0.97897629, 0.        ],\n",
       "       [0.91882603, 0.        ],\n",
       "       [0.33453557, 1.        ],\n",
       "       [0.19277561, 1.        ],\n",
       "       [0.98437123, 0.        ],\n",
       "       [0.30799904, 1.        ],\n",
       "       [0.97467291, 0.        ],\n",
       "       [0.82863004, 0.        ],\n",
       "       [0.98836025, 0.        ],\n",
       "       [0.98635236, 0.        ],\n",
       "       [0.99356113, 0.        ],\n",
       "       [0.98185249, 0.        ],\n",
       "       [0.98962553, 0.        ],\n",
       "       [0.99472994, 0.        ],\n",
       "       [0.98364025, 0.        ],\n",
       "       [0.25321587, 1.        ],\n",
       "       [0.96615992, 0.        ],\n",
       "       [0.1725864 , 1.        ],\n",
       "       [0.84686297, 0.        ],\n",
       "       [0.8059043 , 0.        ],\n",
       "       [0.96376236, 0.        ],\n",
       "       [0.97274431, 0.        ],\n",
       "       [0.96379876, 0.        ],\n",
       "       [0.41977396, 1.        ],\n",
       "       [0.38186108, 1.        ],\n",
       "       [0.99307436, 0.        ],\n",
       "       [0.97316145, 0.        ],\n",
       "       [0.97316873, 0.        ],\n",
       "       [0.98540759, 0.        ],\n",
       "       [0.183512  , 1.        ],\n",
       "       [0.97817831, 0.        ],\n",
       "       [0.97449098, 0.        ],\n",
       "       [0.16310949, 1.        ],\n",
       "       [0.95502482, 0.        ],\n",
       "       [0.98620851, 0.        ],\n",
       "       [0.94154816, 0.        ],\n",
       "       [0.86945677, 0.        ],\n",
       "       [0.92187637, 0.        ],\n",
       "       [0.96011388, 0.        ],\n",
       "       [0.99101554, 0.        ],\n",
       "       [0.99336897, 0.        ],\n",
       "       [0.9598906 , 0.        ],\n",
       "       [0.9944311 , 0.        ],\n",
       "       [0.97628708, 0.        ],\n",
       "       [0.20317902, 1.        ],\n",
       "       [0.9495559 , 0.        ],\n",
       "       [0.43032919, 1.        ],\n",
       "       [0.99696019, 0.        ],\n",
       "       [0.98256803, 0.        ],\n",
       "       [0.11979382, 1.        ],\n",
       "       [0.91999864, 0.        ],\n",
       "       [0.85357773, 0.        ],\n",
       "       [0.99119513, 0.        ],\n",
       "       [0.99221554, 0.        ],\n",
       "       [0.98104966, 0.        ],\n",
       "       [0.26078667, 1.        ],\n",
       "       [0.97716556, 0.        ],\n",
       "       [0.98983208, 0.        ],\n",
       "       [0.0976858 , 1.        ],\n",
       "       [0.96796874, 0.        ],\n",
       "       [0.97982774, 0.        ],\n",
       "       [0.96882419, 0.        ],\n",
       "       [0.98055015, 0.        ],\n",
       "       [0.98638421, 0.        ],\n",
       "       [0.48235585, 1.        ],\n",
       "       [0.1134434 , 1.        ],\n",
       "       [0.98187048, 0.        ],\n",
       "       [0.98394766, 0.        ],\n",
       "       [0.99026569, 0.        ],\n",
       "       [0.996799  , 0.        ],\n",
       "       [0.61008577, 0.        ],\n",
       "       [0.95979426, 0.        ],\n",
       "       [0.99366612, 0.        ],\n",
       "       [0.9852662 , 0.        ],\n",
       "       [0.97141509, 0.        ],\n",
       "       [0.39598987, 1.        ],\n",
       "       [0.98727346, 0.        ],\n",
       "       [0.98757971, 0.        ],\n",
       "       [0.9815314 , 0.        ],\n",
       "       [0.98075704, 0.        ],\n",
       "       [0.67864172, 0.        ],\n",
       "       [0.431966  , 1.        ],\n",
       "       [0.44488585, 1.        ],\n",
       "       [0.98088751, 0.        ],\n",
       "       [0.99617438, 0.        ],\n",
       "       [0.92149007, 0.        ],\n",
       "       [0.28040557, 1.        ],\n",
       "       [0.97803367, 0.        ],\n",
       "       [0.25227921, 1.        ],\n",
       "       [0.92614743, 0.        ],\n",
       "       [0.26186677, 1.        ],\n",
       "       [0.92026492, 0.        ],\n",
       "       [0.63409446, 0.        ],\n",
       "       [0.9859147 , 0.        ],\n",
       "       [0.99186171, 0.        ],\n",
       "       [0.99019429, 0.        ],\n",
       "       [0.97005372, 0.        ],\n",
       "       [0.98069354, 0.        ],\n",
       "       [0.98996557, 0.        ],\n",
       "       [0.97567788, 0.        ],\n",
       "       [0.08359259, 1.        ],\n",
       "       [0.98375262, 0.        ],\n",
       "       [0.97375284, 0.        ],\n",
       "       [0.62535752, 0.        ],\n",
       "       [0.71280098, 0.        ],\n",
       "       [0.9637814 , 0.        ],\n",
       "       [0.98967408, 0.        ],\n",
       "       [0.98996297, 0.        ],\n",
       "       [0.02867912, 1.        ],\n",
       "       [0.99648826, 0.        ],\n",
       "       [0.98462378, 0.        ],\n",
       "       [0.9882957 , 0.        ],\n",
       "       [0.98285571, 0.        ],\n",
       "       [0.35479049, 1.        ],\n",
       "       [0.35476437, 1.        ],\n",
       "       [0.99690783, 0.        ],\n",
       "       [0.18289298, 1.        ],\n",
       "       [0.81381985, 0.        ],\n",
       "       [0.39018056, 1.        ],\n",
       "       [0.21202455, 1.        ],\n",
       "       [0.99032108, 0.        ],\n",
       "       [0.16516077, 1.        ],\n",
       "       [0.29983704, 1.        ],\n",
       "       [0.96051864, 0.        ],\n",
       "       [0.59205595, 0.        ],\n",
       "       [0.9968644 , 0.        ],\n",
       "       [0.99073938, 0.        ],\n",
       "       [0.9964219 , 0.        ],\n",
       "       [0.98281093, 0.        ],\n",
       "       [0.89819537, 0.        ]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 6 结果输出\n",
    "## 6.1 test集的预测\n",
    "y_test_pred = gbm.predict_proba(test_Base.drop(['policy_id'],axis=1))\n",
    "y_test_pred[:, 1][y_test_pred[:, 1] > 0.5] = '1'\n",
    "y_test_pred[:, 1][y_test_pred[:, 1] <= 0.5] = '0'\n",
    "y_test_pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 6.2 输出结果\n",
    "\n",
    "result = pd.read_csv('./data/submission.csv')\n",
    "result['fraud'] = y_test_pred[:, 1]\n",
    "result.to_csv(f'./data/{model_name}.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>model_name</th>\n",
       "      <th>update_time</th>\n",
       "      <th>Accuracy</th>\n",
       "      <th>Precision</th>\n",
       "      <th>Recall</th>\n",
       "      <th>F1 Score</th>\n",
       "      <th>auc</th>\n",
       "      <th>sub_score</th>\n",
       "      <th>update_content</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>model_0_86224_base</td>\n",
       "      <td>2024/4/26 11:55</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>base model</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           model_name      update_time  Accuracy  Precision  Recall  F1 Score  \\\n",
       "0  model_0_86224_base  2024/4/26 11:55         1          1       1         1   \n",
       "\n",
       "   auc  sub_score update_content  \n",
       "0    1          1     base model  "
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 7 模型评估结果输出\n",
    "evalue_result=pd.read_csv('./data/evalue_result.csv', encoding='utf-8')\n",
    "evalue_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 5.1 模型评估结果输出\n",
    "import datetime \n",
    "\n",
    "new_row = {'model_name': model_name, 'update_time': datetime.datetime.now() , 'Accuracy': accuracy, 'Precision': precision, 'Recall': recall\n",
    " , 'F1 Score': f1, 'auc': auc, 'sub_score': 0.8914, 'update_content': 'delete# 1.2 1.3 1.4 2.0(date-diff) ; add #3.1(data-split);update ## 3.2-test_size-random_state '}  \n",
    "evalue_result.loc[len(evalue_result.index)] = new_row \n",
    "evalue_result\n",
    "evalue_result.to_csv('./data/evalue_result.csv', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
